package controllers;

import org.springframework.util.StringUtils;

import play.mvc.*;
import play.mvc.Http.*;

import models.*;

public class AdminSecured extends Security.Authenticator {
    
    @Override
    public String getUsername(Context ctx) {
    	String email = ctx.session().get("email");
    	boolean isAdmin = isAdminUser(email);
        if (StringUtils.isEmpty(email)) {
        	return null;
        } else if (!isAdmin) {
        	return null;
        } else {
        	return email;
        }
    }
    
    @Override
    public Result onUnauthorized(Context ctx) {
        return redirect(routes.Application.login());
    }
    
    public static boolean isAdminUser(String email) {
    	Account user = Account.findByName(email);
    	return true;
    }
}